package com.aft.terra.common.utils;

import com.aft.terra.app.Constants;
import com.aft.terra.modules.game.model.User;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

public class RedisKit {

    private RedisKit() {

    }

    private static Cache cache;

    private static String dbName;

    static {
        dbName = PropKit.use(Constants.RES_JDBC_FILE_NAME).get(Constants.REDIS_DB_NAME);
        cache = use(dbName);
    }

    /**
     * 获取用户信息
     *
     * @return
     * @author Mandarava
     */
    /*public static User getUser(String userId) {
		if (cache == null)
			cache = Redis.use(dbName);
		return cache.get(userId);
	}*/

//	/**
//	 * 获取商家信息
//	 * 
//	 * @param accountId
//	 * @return
//	 */
//	public static Account getAccount(String accountId) {
//		if (cache == null)
//			cache = Redis.use(dbName);
//		return JSON.parseObject((String) cache.get(accountId), Account.class);
//	}
    public static Cache use(String dbName) {
        return (cache = com.jfinal.plugin.redis.Redis.use(dbName));
    }

    public static Boolean ping(Cache cache) {
        try {
            if (cache.ping().equals("PONG"))
                return true;
            else
                return false;
        } catch (Exception e) {
            return false;
        }
    }

    public static void del(String key) {
        if (cache == null)
            cache = Redis.use(dbName);
        if (ping(cache)) {
            cache.del(key);
        }
    }

    public static void rpush(String key, Object value) {
        if (cache == null)
            cache = Redis.use(dbName);
        if (ping(cache)) {
            cache.rpush(key, value);
        }
    }

    public static Cache getCache() {
        if (cache == null)
            cache = Redis.use(dbName);
        return cache;
    }

    public static User getUser(String key) {
        if(cache==null)
            cache=Redis.use(dbName);
        return cache.get(key);
    }
}
